package org.vectrics.recruiter.struts;

import org.apache.log4j.Logger;
import org.vectrics.common.util.StringUtils;
import org.vectrics.company.CompanySelectCriteria;
import org.vectrics.domain.Domain;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.config.RecruitConfig;
import org.vectrics.recruiter.job.JobSearchCriteria;
import org.vectrics.recruiter.struts.admin.JobSearchActionHelper;
import org.vectrics.recruiter.struts.tag.JobDetailDisplayOptions;
import org.vectrics.struts.VectricsActionData;
import org.vectrics.struts.VectricsStrutsAction;

public class JobSearchAction extends VectricsStrutsAction {
   
	private transient Logger log = Logger.getLogger(this.getClass());
	public static String ACTION_NAME = "search_job";
	public static String LOCATION_PARAM = "active";
	public static String FUNCTION_PARAM = "func";
	public static String SEARCH_WORD_PARAM = "search_phrase";

	protected void setDefaultFormProperties(JobSearchForm jobSearchForm) {
	}
	
	public String doExecute(VectricsActionData actionData) {
		String forward = "success";
		
		log.debug("JobSearchAction.excecute() called");
		
		boolean cancelButtonPressed = actionData.wasButtonPressed("cancel");
		JobSearchActionHelper helper = new JobSearchActionHelper();
		Domain domain = ThreadProperties.getDomain();
		String reset= actionData.getRequest().getParameter("reset");
		if (reset == null) {
			reset = actionData.getRequest().getParameter("reset.x");
	    }
		
		if (reset != null) {	
			log.debug("doPerform() submit button pressed - saving..");
			log.debug("Form class = " + actionData.getForm().getClass().getName());
			JobSearchForm jobSearchForm = (JobSearchForm)actionData.getForm();
			jobSearchForm = new JobSearchForm();
			actionData.setForm(jobSearchForm);
			JobSearchCriteria criteria = helper.formToCriteria(jobSearchForm, actionData.getRequest());
			helper.storeCriteria(actionData.getRequest(), criteria);
			actionData.getRequest().setAttribute("requester", ""); 	
			actionData.getRequest().setAttribute("primaryRecruiter", ""); 	
			actionData.getRequest().setAttribute("location", ""); 	
			return "cancel";
		}
		
		if (!cancelButtonPressed) {
			log.debug("doPerform() submit button pressed - saving..");
			log.debug("Form class = " + actionData.getForm().getClass().getName());
			JobSearchForm jobSearchForm = (JobSearchForm)actionData.getForm();
		    
			if (actionData.getForm() == null) {
				log.debug("Form was not passed to action, creating form");
				jobSearchForm = new JobSearchForm();
			}

			setDefaultFormProperties(jobSearchForm);
			
			// Seteo el requester para que lo tome el picker
			if ((jobSearchForm.getRequester()!= null)&&(!jobSearchForm.getRequester().trim().equals(""))) {
				actionData.getRequest().setAttribute("requester", jobSearchForm.getRequester()); 				
			}

			// Seteo el Primary Recruiter para que lo tome el picker
			if ((jobSearchForm.getPrimaryRecruiter() != null)&&(!jobSearchForm.getPrimaryRecruiter().trim().equals(""))) {
				actionData.getRequest().setAttribute("primaryRecruiter", jobSearchForm.getPrimaryRecruiter());				
			}
			
			// Seteo el id de location para que lo tome el combo
			if ((jobSearchForm.getLocation() != null)&&(!jobSearchForm.getLocation().trim().equals(""))) {
				actionData.getRequest().setAttribute("location", jobSearchForm.getLocation());				
			}
			
			if (jobSearchForm.getShowPdfLink() == null) {
				jobSearchForm.setShowPdfLink("true");
			}
			
			String needApprovalParam = actionData.getRequest().getParameter("needApproval");
			if ((StringUtils.isNullOrBlank(needApprovalParam) == false) && (needApprovalParam.startsWith("true"))) {
				jobSearchForm.setShowApprovalGroup("true");
			}
			
			if (actionData.getRequest().getParameter("showCompanyJobs") != null) {
				log.debug("Show company jobs");
				jobSearchForm.setCompanyId(actionData.getRequest().getParameter("showCompanyJobs"));
				jobSearchForm.setShowBlock("false");
				jobSearchForm.setShowActiveStatus("true");
				jobSearchForm.setShowCompany("false");
				jobSearchForm.setShowCompanyCode("false");
			} else {
				if ((jobSearchForm.getShowCompany() == null) 
							&& (jobSearchForm.getShowCompanyCode() == null)) {
					jobSearchForm.setShowCompanyCode("true");
				}
			}
			
			log.debug("Finding criteria and attaching to session attribute");
			JobSearchCriteria criteria = helper.formToCriteria(jobSearchForm, actionData.getRequest());
			helper.storeCriteria(actionData.getRequest(), criteria);
			
			log.debug("Finding display options and attaching to session attribute");
			JobDetailDisplayOptions displayOptions = helper.formToDisplayOptions(jobSearchForm);
			if (RecruitConfig.getRecruitInstance().getShowJobListCompanyCode(ThreadProperties.getDomain()) == true)
				displayOptions.setShowCompanyCode(true);
			else
				displayOptions.setShowCompanyCode(false);

			int companyCount = findCompanyCountForDomain(domain);

			if (RecruitConfig.getRecruitInstance().getShowJobListCompanyName(domain) == true) {
				displayOptions.setShowCompany(true);
			} else {
				displayOptions.setShowCompany(false);
			}
		
			if ((needApprovalParam != null) && (needApprovalParam.startsWith("true"))) {
				displayOptions.setShowApprovalGroups(true);
			}
			
			if (RecruitConfig.getRecruitInstance().getShowJobListIcon(domain) == true)
				displayOptions.setShowActiveIndicator(true);
			else
				displayOptions.setShowActiveIndicator(false);
			
			if (RecruitConfig.getRecruitInstance().getShowJobListReferenceCode(domain) == true)
				displayOptions.setShowReferenceCode(true);
			else
				displayOptions.setShowReferenceCode(false);
			
			if (log.isDebugEnabled())
				log.debug("Display options = " + displayOptions);
			
			helper.storeDisplayOptions(actionData.getRequest(), displayOptions);
		} else {
			log.debug("Cancelled action");
			forward = "cancel";
		}
		return (forward);
	}
	
	private int findCompanyCountForDomain(Domain domain) {
		CompanySelectCriteria criteria = new CompanySelectCriteria();
		criteria.addDomain(domain);
		int count = CoreServiceLocator.getCompanyService().findCompanyCount(criteria);
		return(count);
	}
	
}
